package lovo.practice_7_14;

import lovo.Util.Util;

import javax.swing.*;
import javax.xml.crypto.dsig.spec.XPathFilter2ParameterSpec;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TransPanel extends JPanel implements ActionListener {
    private J189_7_14 frame;
    private JButton jButtonTrans;
    private JTextField jTextFieldwho;
    private JTextField jTextFieldhow;
    private JButton jButtonBack;
    private Icon imgback = Util.getimagesIcon("/Images/buttonOff.jpg");

    public TransPanel(J189_7_14 frame) {
        this.frame = frame;
        this.setLayout(null);
        this.setOpaque(false);
        jButtonTrans = new JButton("转账");
        jButtonTrans.setBounds(200, 200, 140, 50);
        this.add(jButtonTrans);

        jTextFieldwho = new JTextField();
        jTextFieldwho.setBounds(400, 200, 140, 50);
        this.add(jTextFieldwho);

        jTextFieldhow = new JTextField();
        jTextFieldhow.setBounds(400, 400, 140, 50);
        this.add(jTextFieldhow);

        jButtonBack = new JButton(imgback);
        jButtonBack.setBounds(600, 600, 140, 50);
        this.add(jButtonBack);
        jButtonTrans.addActionListener(this);
        jTextFieldwho.addActionListener(this);
        jTextFieldhow.addActionListener(this);
        jButtonBack.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        int a = 0;
        if (e.getSource() == jButtonTrans) {
            for (int i = 0; i < frame.getListusers().size(); i++) {
                if (jTextFieldwho.getText().equals(frame.getListusers().get(i).getAccount())) {
                    a = 2;
                    if (frame.getListusers().get(i).getAccount().equals(frame.getUsers().getAccount())) {
                        JOptionPane.showMessageDialog(this, "不能给自己转账");
                        break;
                    } else {
                        frame.setI(i);
                        frame.getListusers().get(i).setBalance(frame.getListusers().get(i).getBalance() + Integer.parseInt(jTextFieldhow.getText()));
                        frame.getUsers().setBalance(frame.getUsers().getBalance()-Integer.parseInt(jTextFieldhow.getText()));
//                        frame.update();
                        frame.getLoginPanel().cha();
                        frame.getLoginPanel().tra();
                        JOptionPane.showMessageDialog(this, "转账成功");
                        break;
                    }
                } else {
                    a = -1;//
                }
            }
            if (a == -1) {
                JOptionPane.showMessageDialog(this, "查无此人");
            }
        }
        if (e.getSource() == jButtonBack) {
            frame.switchPanel("mune");
        }
    }
}
